/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.plugins.usereflect;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zxy.business.Ope_DB_Cent;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.db1.Db_Common1;

//元数据视图创建业务
public class A01_YYNNKK_IN implements com.zxy.common.ParamName_InterFace
{
	public StringBuilder	strResult		= new StringBuilder();
	public String			user_main_id			= null;
	public String			session_id		= null;
	public String			param_name		= "";
	public String			param_value1	= null;
	public String			param_value2	= null;
	public String			param_value3	= null;
	public String			param_value4	= null;
	public String			param_value5	= null;
	public String			param_value6	= null;
	public String			param_value7	= null;
	public String			param_value8	= null;
	public String			param_value9	= null;
	public String			param_value10	= null;

	@Override
	public void init_start()
	{
		StringBuilder strSql = new StringBuilder();
		try
		{
			Ope_DB_Cent opc = new Ope_DB_Cent();
			JSONArray jso_single_table = this.get_t_single_table();
			int DB_ID = Integer.valueOf(jso_single_table.getJSONObject(0).getString("DB_ID"));
			// 表存在
			if(jso_single_table.length() > 0)
			{
				strSql.delete(0,strSql.length());
				strSql.append("select * from t_table_column where TABLE_ID ='"
						+ this.param_value1 + "' order by S_SORT,MAIN_ID asc");
				JSONArray temListtable_info = opc.SqlToJson(1,strSql.toString());
				String temTableName = jso_single_table.getJSONObject(0).getString("TABLE_EN_NAME");
				String temTableType = jso_single_table.getJSONObject(0).getString("TABLE_TYPE");
				String temViewSql = jso_single_table.getJSONObject(0).getString("S_DESC");
				// 各种条件判断
				String temCondAva = this.CondAva(temTableName,temListtable_info);
				if(!java.util.Objects.equals(temCondAva,""))
				{
					this.strResult.delete(0,this.strResult.length());
					this.strResult.append(Com_Fun.get_ope_result(param_name,"0","元数据表创建业务失败:" + temCondAva));
				}
				// 视图模式生成
				else if(java.util.Objects.equals(temTableType,"1"))
				{
					A01_STATICDB4_IN a01_sdb = new A01_STATICDB4_IN();
					a01_sdb.param_name = this.param_name;//"A01_STATICDBI";
					a01_sdb.param_value1 = String.valueOf(DB_ID);
					a01_sdb.param_value2 = temTableName;
					a01_sdb.param_value3 = jso_single_table.getJSONObject(0).getString("TABLE_CN_NAME");
					a01_sdb.param_value5 = temViewSql;
					a01_sdb.param_value9 = this.param_value2;
					a01_sdb.param_value10 = "1";
					a01_sdb.table_info = temListtable_info;
					a01_sdb.init_start();
					if(a01_sdb.strResult.length() > 0){
						this.strResult = a01_sdb.strResult;
					}
					else{
						this.strResult.delete(0,this.strResult.length());
						this.strResult.append(Com_Fun.get_ope_result(param_name,"1","元数据视图创建业务成功"));
					}
				}else if(java.util.Objects.equals(temTableName,""))
				{
					this.strResult.delete(0,this.strResult.length());
					this.strResult.append(Com_Fun.get_ope_result(param_name,"0","元数据表创建业务失败，元数据表名不能为空"));
				}else if(!temCondAva.equals(""))
				{
					this.strResult.delete(0,this.strResult.length());
					this.strResult.append(Com_Fun.get_ope_result(param_name,"0","元数据表创建业务失败，" + temCondAva));
				}
				// 表模式生成
				else
				{
					A01_STATICDB3_IN a01_sdb = new A01_STATICDB3_IN();
					a01_sdb.param_name = this.param_name;//"A01_STATICDBI";
					a01_sdb.param_value1 = String.valueOf(DB_ID);
					a01_sdb.param_value2 = temTableName;
					a01_sdb.param_value3 = jso_single_table.getJSONObject(0).getString("TABLE_CN_NAME");
					a01_sdb.param_value4 = "";
					a01_sdb.param_value9 = this.param_value2;
					a01_sdb.param_value10 = "1";
					a01_sdb.table_info = temListtable_info;
					a01_sdb.init_start();
					if(a01_sdb.strResult.length() > 0){
						this.strResult = a01_sdb.strResult;
					}
					else{
						this.strResult.delete(0,this.strResult.length());
						this.strResult.append(Com_Fun.get_ope_result(param_name,"1","元数据表创建业务成功"));
					}
				}
			}else
			{
				this.strResult.delete(0,this.strResult.length());
				this.strResult.append(Com_Fun.get_ope_result(param_name,"0","元数据表创建业务失败，元数据表记录不存在"));
			}
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString(),strSql.toString());
			this.strResult.delete(0,this.strResult.length());
			this.strResult.append(Com_Fun.get_ope_result(param_name,"0","异常:"+e.getMessage()));
		}
	}

	// 各类前置条件是否有效
	private String CondAva(String inputTableName, JSONArray inputListtable_info) throws JSONException
	{
		for(int i = 0;i < inputListtable_info.length();i++)
		{
			JSONObject temt_table_column = inputListtable_info.getJSONObject(i);
			if(java.util.Objects.equals(temt_table_column.getString("COLUMN_QRY"),"3") 
					&& java.util.Objects.equals(temt_table_column.getString("COLUMN_TYPE"),2) 
					&& (java.util.Objects.equals(temt_table_column.getString("COLUMN_QRY_FORMAT").trim(),"")
							|| java.util.Objects.equals(temt_table_column.getString("COLUMN_FIND_HTML").trim(),"")
							|| java.util.Objects.equals(temt_table_column.getString("COLUMN_FIND_RETURN").trim(),"")))
				return temt_table_column.getString("COLUMN_CN_NAME") + "["
						+ temt_table_column.getString("COLUMN_EN_NAME")
						+ "]下拉框或查找框数据集,查找框子页面,查找框返回值均不能为空";
			else if(java.util.Objects.equals(temt_table_column.getString("COLUMN_QRY").trim(),"2")
					&& java.util.Objects.equals(temt_table_column.getString("COLUMN_QRY_FORMAT").trim(),""))
				return temt_table_column.getString("COLUMN_CN_NAME") + "["
						+ temt_table_column.getString("COLUMN_EN_NAME")
						+ "]下拉框或查找框数据集不能为空";
			else if(!java.util.Objects.equals(temt_table_column.getString("COLUMN_QRY_FORMAT").trim(),"")
					&& temt_table_column.getString("COLUMN_QRY_FORMAT").toUpperCase().indexOf("SELECT ") == 0)
				return "";
			else if(!java.util.Objects.equals(temt_table_column.getString("COLUMN_QRY_FORMAT").trim(),""))
			{
				try
				{
					JSONArray jsonObject = new JSONArray(temt_table_column.getString("COLUMN_QRY_FORMAT"));
					return "";
				}
				catch(Exception e)
				{
					Ope_DB_Cent odc = new Ope_DB_Cent();
					if(odc.get_t_proc_name_id(temt_table_column.getString("COLUMN_QRY_FORMAT")).equals(""))
						return temt_table_column.getString("COLUMN_CN_NAME")
								+ "["
								+ temt_table_column.getString("COLUMN_EN_NAME")
								+ "]下拉框或查找框数据集数据接口"
								+ temt_table_column.getString("COLUMN_QRY_FORMAT")
								+ "不存在";

				}
			}
		}
		return "";
	}

	private JSONArray get_t_single_table()
	{
		Db_Common1 dbc1 = null;
		StringBuilder strSql = new StringBuilder();
		JSONArray jso = new JSONArray();
		ResultSet rs = null;
		try
		{
			dbc1 = new Db_Common1();
			strSql.append("select * from t_single_table where main_id = '"
					+ this.param_value1 + "'");
			rs = dbc1.Common_Sql(strSql);
			jso = Com_Fun.ResultSetToJSON(rs);
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString(),strSql.toString());
		}
		finally
		{
			try
			{
				if(rs != null)
				{
					rs.getStatement().close();
					rs.close();
				}
			}
			catch(SQLException e)
			{
			}
		}
		return jso;
	}

	/**
	 * @return strResult
	 */
	public StringBuilder getStrResult()
	{
		return strResult;
	}

	/**
	 * @param strResult
	 *            要设置的 strResult
	 */
	public void setStrResult(StringBuilder strResult)
	{
		this.strResult = strResult;
	}

	/**
	 * @return user_main_id
	 */
	public String getUser_main_id()
	{
		return user_main_id;
	}

	/**
	 * @param user_main_id
	 *            要设置的 user_main_id
	 */
	public void setUser_main_id(String user_main_id)
	{
		this.user_main_id = user_main_id;
	}

	/**
	 * @return session_id
	 */
	public String getSession_id()
	{
		return session_id;
	}

	/**
	 * @param session_id
	 *            要设置的 session_id
	 */
	public void setSession_id(String session_id)
	{
		this.session_id = session_id;
	}

	/**
	 * @return param_name
	 */
	public String getParam_name()
	{
		return param_name;
	}

	/**
	 * @param param_name
	 *            要设置的 param_name
	 */
	public void setParam_name(String param_name)
	{
		this.param_name = param_name;
	}

	/**
	 * @return param_value1
	 */
	public String getParam_value1()
	{
		return param_value1;
	}

	/**
	 * @param param_value1
	 *            要设置的 param_value1
	 */
	public void setParam_value1(String param_value1)
	{
		this.param_value1 = param_value1;
	}

	/**
	 * @return param_value2
	 */
	public String getParam_value2()
	{
		return param_value2;
	}

	/**
	 * @param param_value2
	 *            要设置的 param_value2
	 */
	public void setParam_value2(String param_value2)
	{
		this.param_value2 = param_value2;
	}

	/**
	 * @return param_value3
	 */
	public String getParam_value3()
	{
		return param_value3;
	}

	/**
	 * @param param_value3
	 *            要设置的 param_value3
	 */
	public void setParam_value3(String param_value3)
	{
		this.param_value3 = param_value3;
	}

	/**
	 * @return param_value4
	 */
	public String getParam_value4()
	{
		return param_value4;
	}

	/**
	 * @param param_value4
	 *            要设置的 param_value4
	 */
	public void setParam_value4(String param_value4)
	{
		this.param_value4 = param_value4;
	}

	/**
	 * @return param_value5
	 */
	public String getParam_value5()
	{
		return param_value5;
	}

	/**
	 * @param param_value5
	 *            要设置的 param_value5
	 */
	public void setParam_value5(String param_value5)
	{
		this.param_value5 = param_value5;
	}

	/**
	 * @return param_value6
	 */
	public String getParam_value6()
	{
		return param_value6;
	}

	/**
	 * @param param_value6
	 *            要设置的 param_value6
	 */
	public void setParam_value6(String param_value6)
	{
		this.param_value6 = param_value6;
	}

	/**
	 * @return param_value7
	 */
	public String getParam_value7()
	{
		return param_value7;
	}

	/**
	 * @param param_value7
	 *            要设置的 param_value7
	 */
	public void setParam_value7(String param_value7)
	{
		this.param_value7 = param_value7;
	}

	/**
	 * @return param_value8
	 */
	public String getParam_value8()
	{
		return param_value8;
	}

	/**
	 * @param param_value8
	 *            要设置的 param_value8
	 */
	public void setParam_value8(String param_value8)
	{
		this.param_value8 = param_value8;
	}

	/**
	 * @return param_value9
	 */
	public String getParam_value9()
	{
		return param_value9;
	}

	/**
	 * @param param_value9
	 *            要设置的 param_value9
	 */
	public void setParam_value9(String param_value9)
	{
		this.param_value9 = param_value9;
	}

	/**
	 * @return param_value10
	 */
	public String getParam_value10()
	{
		return param_value10;
	}

	/**
	 * @param param_value10
	 *            要设置的 param_value10
	 */
	public void setParam_value10(String param_value10)
	{
		this.param_value10 = param_value10;
	}
}
